//
// Created by 文道江 on 15/11/20.
// Copyright (c) 2015 文道江. All rights reserved.
//


#ifndef __HYBRID_BRIDGE_HEADER__
#define __HYBRID_BRIDGE_HEADER__

#define HYBRID_DISPATCH_QUEUE_NAME "my.concurrent.queue"

#if defined(DEBUG)||defined(_DEBUG)
#define IS_DEBUG YES
#else
#define IS_DEBUG NO
#endif

#ifndef __OPTIMIZE__
#define NSLog(...) NSLog(__VA_ARGS__)
#else
#define NSLog(...){}
#endif

#import <UIKit/UIKit.h>
#import "AFNetworking/UIKit+AFNetworking.h"
#import "TBCityIconFont.h"
#import "WebViewClient.h"
#import "AFNetworkReachabilityManager.h"
#import "MAThemeKit.h"
#import "KSToastView.h"
#import "MBProgressHUD.h"
#import "KVStorage.h"

typedef void (^ResultHandler)(BOOL resultStatus, id resultData);
typedef void (^EventBusHandler)(NSNotification *notification);

@interface AppTheme : NSObject
    @property (strong) UIColor *primaryColor;
    @property (strong) UIColor *color;
    @property BOOL lightStatusBar;

- (instancetype)initWithPrimaryColor:(UIColor *)primaryColor color:(UIColor *)color lightStatusBar:(BOOL)lightStatusBar;

+ (instancetype)themeWithPrimaryColor:(UIColor *)primaryColor color:(UIColor *)color lightStatusBar:(BOOL)lightStatusBar;

@end



@interface HybridBridge : NSObject

@property (strong, nonatomic) NSMutableDictionary *plugins;
@property (strong, nonatomic) UIViewController *controller;
@property (strong, nonatomic) UIView *view;
@property (strong, nonatomic) WebViewClient *webViewClient;
@property (strong, nonatomic) WVJB_WEBVIEW_TYPE *webView;
@property (strong, nonatomic) WKWebViewJavascriptBridge *javascriptBridge;
@property (strong, nonatomic) NSDictionary *appInfo;
@property (nonatomic) AppTheme *theme;

+ (instancetype)bridgeForWebView:(WVJB_WEBVIEW_TYPE*)webView webViewDelegate:(WVJB_WEBVIEW_DELEGATE_TYPE*)webViewDelegate controller:(UIViewController *)controller handler:(WVJBHandler)messageHandler ;
+ (instancetype)bridgeForWebView:(WVJB_WEBVIEW_TYPE*)webView webViewDelegate:(WVJB_WEBVIEW_DELEGATE_TYPE*)webViewDelegate handler:(WVJBHandler)messageHandler;

+ (instancetype)defaultHybridBridge;

- (instancetype)initWithWebView:(WVJB_WEBVIEW_TYPE*)webView webViewDelegate:(WVJB_WEBVIEW_DELEGATE_TYPE*)webViewDelegate controller:(UIViewController *)controller handler:(WVJBHandler)messageHandler;
- (instancetype)initWithWebView:(WVJB_WEBVIEW_TYPE*)webView webViewDelegate:(WVJB_WEBVIEW_DELEGATE_TYPE*)webViewDelegate handler:(WVJBHandler)messageHandler;
- (void)execute:(id)data Handler:(WVJBResponseCallback) responseCallback WVJBHandler:(WVJBHandler)messageHandler;
- (void)executeAction:(NSString *)action Params:(id)params Handler:(WVJBResponseCallback) responseCallback;
- (void)loadPlugin:(NSString *)pluginName;

@end

#define APP_EVENT_LAUNCHING_FINISH @"app_launching_finish"
#define APP_EVENT_BECOME_ACTIVE @"applicationDidBecomeActive"
#define APP_EVENT_DID_ENTER_BACKGROUND @"applicationDidEnterBackground"
#define APP_EVENT_WILL_ENTER_FOREGROUND @"applicationWillEnterForeground"
#define APP_EVENT_WILL_RESIGN_ACTIVE @"applicationWillResignActive"
#define APP_EVENT_REGISTER_USER_NOTIFICATION_SETTINGS @"didRegisterUserNotificationSettings"
#define APP_EVENT_REGISTER_FOR_REMOTE_NOTIFICATIONS_WITH_DEVICE_TOKEN @"didRegisterForRemoteNotificationsWithDeviceToken"
#define APP_EVENT_FAIL_TO_REGISTER_FOR_REMOTE_NOTIFICATIONS_WITH_ERROR @"didFailToRegisterForRemoteNotificationsWithError"
#define APP_EVENT_RECEIVE_REMOTE_NOTIFICATION @"didReceiveRemoteNotification"
#define APP_EVENT_RECEIVE_LOCAL_NOTIFICATION @"didReceiveLocalNotification"
#define APP_EVENT_PERFORM_FETCH_WITH_COMPLETION_HANDLER @"performFetchWithCompletionHandler"

@interface HybridEventBus : NSObject

@property (strong, nonatomic) NSDictionary *launchOptions;

+ (HybridEventBus *)defaultEventBus;

- (void)subscribe:(NSString *)tag UsingBlock:(EventBusHandler)handler;
- (void)subscribeUsingBlock:(EventBusHandler)handler;

- (void)postData:(NSDictionary *)data Tag:(NSString *)tag;
- (void)postData:(NSDictionary *)data;

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName object:(nullable id)anObject;

- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(nullable NSNotificationName)aName object:(nullable id)anObject;
@end

@interface HybridHandler : NSObject

@property (strong, nonatomic)UIViewController *controller;
@property (strong, nonatomic)UIView *view;
@property (strong, nonatomic)WVJB_WEBVIEW_TYPE *webView;
@property (strong, nonatomic)HybridBridge *bridge;

+ (instancetype)handlerWithWebView:(WVJB_WEBVIEW_TYPE*)webView controller:(UIViewController *)controller;
- (instancetype)initWithWebView:(WVJB_WEBVIEW_TYPE*)webView controller:(UIViewController *)controller;

- (void) Run:(id) data callback:(ResultHandler)callback;

- (void)send:(id)data ;
- (void)send:(id)data responseCallback:(WVJBResponseCallback)responseCallback ;
- (void)callHandler:(NSString *)handlerName ;
- (void)callHandler:(NSString *)handlerName data:(id)data ;
- (void)callHandler:(NSString *)handlerName data:(id)data responseCallback:(WVJBResponseCallback)responseCallback ;
- (void)dispatchEvent:(NSString *)eventName data:(id)data ;
- (void)reset;

@end


typedef void (^ResultHandler)(BOOL resultStatus, id resultData);

@interface HybridAction : NSObject

@property (strong, nonatomic)UIViewController *controller;
@property (strong, nonatomic)UIView *view;
@property (strong, nonatomic)WVJB_WEBVIEW_TYPE *webView;
@property (strong, nonatomic) HybridHandler *hybridHandler;
@property (strong, nonatomic) ResultHandler resultHandler;

+ (instancetype)actionWithWebView:(WVJB_WEBVIEW_TYPE*)webView controller:(UIViewController *)controller;
- (instancetype)initWithWebView:(WVJB_WEBVIEW_TYPE*)webView controller:(UIViewController *)controller;
- (void)actionInitialize;

- (void)callback:(BOOL)status data:(id)data;
- (void)dispatchEvent:(NSString *)eventName data:(id)data ;
- (void)callHandler:(NSString *)handlerName data:(id)data responseCallback:(WVJBResponseCallback)responseCallback ;
@end


@protocol HybridPluginProtocol
@optional

- (void)pluginInitialize;

- (void)enterBackground;

- (void)enterForeground;

- (void)didBecomeActive;

- (void)willResignActive;

- (void)didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings;

- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;

- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;

- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;

- (void)didReceiveLocalNotification:(UILocalNotification *)notification;

- (void)performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;

@end

@interface HybridPlugin : HybridHandler <HybridPluginProtocol>


@end

@interface HybridActionHandler : HybridHandler

@end

#endif